var app = getApp()
var wxParse = require('../../../templates/wxParse/wxParse.js');
var auth = require('../../../templates/auth/auth.js');
var getAppGlobalData = require('../../../templates/copyright/copyright.js');
var timer;
Page({
  data: {
    swiperIndexCur: 1,
    swiperStatus: {
      indicatorDots: false,
      autoplay: true
    },
    detailID: '',
    to_uid: '',
    from_id: '',
    isStaff: '',
    detailData: {},
    globalData: {},
    collStatus: '',
    authStatus: true,
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(this, options, "1111111111111111111111111111111111111111111111111111111111111111");
    var that = this;
    getAppGlobalData.getAppGlobalData(that);
    wx.showShareMenu({
      withShareTicket: true,
      success: function (res) {
        // 分_享成功
        console.log('shareMenu share success')
        console.log('分_享' + res)
      },
      fail: function (res) {
        // 分_享失败
        console.log(res)
      }
    })


    if (options.id) {
      that.setData({
        detailID: options.id
      })
    }

    if (options.to_uid) {
      that.setData({
        to_uid: options.to_uid
      })
      app.globalData.to_uid = that.data.to_uid;
    }
    console.log(options.to_uid, "options.to_uid***********", app.globalData.to_uid)

    if (options.from_id) {
      that.setData({
        from_id: options.from_id
      })
      app.globalData.from_id = that.data.from_id;
    }
    if (options.isStaff) {
      that.setData({
        isStaff: options.isStaff
      })
      app.globalData.isStaff = that.data.isStaff;
    }

    if (wx.getStorageSync("formID")) {
      let formID = wx.getStorageSync("formID");
      formID = that.data.from_id;
      wx.setStorageSync("formID", formID);
    } else {
      wx.setStorageSync("formID", that.data.from_id);
    }

    that.getClientPhone();
    that.getProductDetail();

    if (that.data.to_uid != wx.getStorageSync("userid")) {
      timer = setInterval(function () {
        console.log(app.globalData.encryptedData, "1111111111111111111111  encryptedData");
        if (app.globalData.encryptedData != '') {
          that.toGetShareInfo();
          clearInterval(timer);
        }
      }, 1000)
    }

  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function (res) {
    // 页面显示 
    var that = this;
    that.checkAuthStatus();
    console.log("onshow that.checkAuthStatus ==> authStatus ", that.data.authStatus)
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
    clearInterval(timer);
  },
  onPullDownRefresh: function () {
    // 监听用户下拉动作
  },
  onReachBottom: function () {
    // 页面上拉触底
  },
  onShareAppMessage: function (res) {
    // 用户点击右上角分_享
    var that = this;
    // if (res.from === 'button') {
    //   console.log("来自页面内转发按钮");
    // }
    // else {
    //   console.log("来自右上角转发菜单")
    // }
    return {
      title: that.data.detailData.name,
      path: '/longbing_card/pages/shop/detail/detail?to_uid=' + app.globalData.to_uid + '&from_id=' + wx.getStorageSync("userid") + '&id=' + that.data.detailID + '&type=2',
      imageUrl: that.data.detailData.images[0],
      success: function (res) {
        console.log(res)
        if (that.data.to_uid != wx.getStorageSync("userid")) {
          that.toForwardRecord();
        }
      },
      fail: function (res) {
        // 分_享失败
        console.log(res)
      }
    };
  },
  getClientPhone: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/UserPhone',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        client_id: wx.getStorageSync("userid")
      },
      success: function (res) {
        // console.log("entry/wxapp/UserPhone ==>", res)
        if (!res.data.errno) {
          if (res.data.data.phone) {
            app.globalData.hasClientPhone = true
            that.setData({
              globalData: app.globalData
            })
          }
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getProductDetail: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/goodsDetail',
      'cachetime': '30',
      'method': 'POST',
      'data': {
        id: that.data.detailID,
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        console.log("entry/wxapp/goodsDetail ==>", res)
        if (!res.data.errno) {
          var tmpData = res.data.data;
          that.setData({
            detailData: tmpData
          })
          // if(that.data.isStaff == 'true'){

          // } else{
          wxParse.wxParse('content', 'html', res.data.data.content, that, 5);
          // }
          if (that.data.detailData.collStatus == 1) {
            that.setData({
              collStatus: 1
            })
          } else if (that.data.detailData.collStatus == 0) {
            that.setData({
              collStatus: 0
            })
          }
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  swiperChange: function (e) {
    var that = this;
    var cur = e.detail.current;
    that.setData({
      swiperIndexCur: cur * 1 + 1
    })
  },
  // 1=>转发名片 2=>转发商品 3=>转发动态 4=>转发公司官网
  // target_id 转发内容的id 当type=2,3时有效
  toForwardRecord: function () {
    var that = this;
    let paramObj = {
      type: 2,
      to_uid: app.globalData.to_uid,
      target_id: that.data.detailID
    }
    console.log("entry/wxapp/Forward ==> paramObj", paramObj)

    app.util.request({
      'url': 'entry/wxapp/Forward',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': paramObj,
      success: function (res) {
        // console.log("entry/wxapp/Forward ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 记录复制文本 拨打电话 查看地址 分_享等情况
  toCopyRecord: function (type) {
    var that = this;
    console.log(app.globalData, "app*********************")
    app.util.request({
      'url': 'entry/wxapp/copyRecord',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        type: type,
        to_uid: app.globalData.to_uid
      },
      success: function (res) {
        // console.log("entry/wxapp/copyRecord ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  toGetShareInfo: function () {
    var that = this;
    // type 1=>浏览名片 2=>浏览自定义码 3=>浏览商品 4=>浏览动态
    // 当type 为2,3,4时, 需要传浏览对象的id 
    wx.login({
      success: function (res) {
        console.log('wx.login ==>>', res);
        let tmpData = {
          encryptedData: app.globalData.encryptedData,
          iv: app.globalData.iv,
          type: 3,
          code: res.code,
          to_uid: that.data.to_uid,
          target_id: that.data.detailID,
        }
        console.log("wx.login == > tmpData ", tmpData)
        app.util.request({
          'url': 'entry/wxapp/getShare',
          'cachetime': '30',
          'showLoading': false,
          'method': 'POST',
          'data': tmpData,
          success: function (res) {
            console.log("entry/wxapp/getShare ==>", res)
            if (!res.data.errno) {
              console.log(res.data.data)
            }
          }
        })
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      console.log("同意授权获取电话号码")
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      console.log(encryptedData, iv)
      that.setPhoneInfo(encryptedData, iv);
    } else if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      console.log("拒绝授权获取电话号码")
    }
    that.checktoConsult();
  },
  setPhoneInfo: function (encryptedData, iv) {
    var that = this;
    console.log(app.globalData.to_uid)
    wx.login({
      success: function (res) {
        console.log('wx.login ==>>', res);
        let tmpData = {
          encryptedData: encryptedData,
          iv: iv,
          code: res.code,
          to_uid: app.globalData.to_uid
        }

        app.util.request({
          'url': 'entry/wxapp/phone',
          'cachetime': '30',
          'method': 'POST',
          'showLoading': false,
          'data': tmpData,
          success: function (res) {
            // console.log("entry/wxapp/update ==>", res)
            if (!res.data.errno) {
              app.globalData.hasClientPhone = true
              that.setData({
                globalData: app.globalData
              })
            }
          }
        })
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  },
  // 检查授权
  checkAuthStatus: function () {
    var that = this;
    auth.checkAuth(that);
  },
  getUserInfo: function (e) {
    var that = this;
    console.log('获取微信用户信息')
    auth.getUserInfo(e);
  },
  checktoConsult: function () {
    var that = this;
    console.log(app.globalData.to_uid, wx.getStorageSync("userid"), app.globalData.nickName, "checktoConsult *********  showModal")
    if (app.globalData.to_uid == 0) {
      wx.showModal({
        title: '',
        content: '不能与默认客服进行对话哦！',
        confirmText: '知道啦',
        showCancel: false,
        success: res => {
          if (res.confirm) {
          } else {
          }
        }
      });
    } else if (app.globalData.to_uid == wx.getStorageSync("userid")) {
      wx.showModal({
        title: '',
        content: '不能和自己进行对话哦！',
        confirmText: '知道啦',
        showCancel: false,
        success: res => {
          if (res.confirm) {
          } else {
          }
        }
      });
    } else {
      wx.navigateTo({
        url: '/longbing_card/chat/userChat/userChat?chat_to_uid=' + app.globalData.to_uid + '&contactUserName=' + app.globalData.nickName
      })
    }
  },
  formSubmit: function (e) {
    var that = this;
    var formId = e.detail.formId;
    var status = e.detail.target.dataset.status;
    that.toSaveFormIds(formId);
    if (status == 'toShop') {
      console.log("商城首页")
      wx.navigateTo({
        url: '/longbing_card/pages/index/index?to_uid=' + app.globalData.to_uid + '&from_id=' + app.globalData.from_id + '&currentTabBar=toShop'
      })
    } else if (status == 'toCollection') {
      console.log("收藏")
      // that.getProductCollection();
    } else if (status == 'toConsult') {
      console.log("去咨询")
      if (app.globalData.to_uid != wx.getStorageSync("userid")) {
        that.toCopyRecord(8);
      }
      if (that.data.globalData.hasClientPhone == true) {
        console.log("用户已授权手机号码")
        that.checktoConsult();
      }
    } else if (status == 'toHome') {
      console.log("回到首页")
      wx.navigateTo({
        url: '/longbing_card/pages/index/index?to_uid=' + app.globalData.to_uid + '&from_id=' + app.globalData.from_id + '&currentTabBar=toCard'
      })
    }
  },
  toSaveFormIds: function (formId) {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/formid',
      'cachetime': '30',
      'showLoading': false,
      'method': 'POST',
      'data': {
        formId: formId
      },
      success: function (res) {
        // console.log("entry/wxapp/formid ==>", res)
        if (!res.data.errno) {
        }
      },
      fail: function (res) {
        console.log("fail ==> ", res)
      }
    })
  }
})